using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using MVCFramework.Models;


namespace MVCFramework.Presenter.Commands
{
   public  class CommandHandler
    {
        private Stack<Command> executedCommands;
        private Stack<Command> unExecutedCommands;
        private Stack<Command> toBeExecutedCommand;

        private int maxStackNumber;
        public int MaxStackNumber
        {
            get { return maxStackNumber; }
            set { maxStackNumber = value; }
        }

        public CommandHandler()
        {
            executedCommands = new Stack <Command>();
            unExecutedCommands = new Stack<Command>();
            toBeExecutedCommand = new Stack<Command>();
            MaxStackNumber = 1000;
        }

        public void AddCommand(Command  command)
        {
            if (executedCommands.Count > MaxStackNumber) return;
            executedCommands.Push(command);
        }

        public void UndoCommands()
        {

        }
        public void RedoCommands()
        {

        }

        /// <summary>
        /// get from executed and add to unexecuted 
        /// </summary>
        /// <param name="number"></param>
        public void UndoCommands(int number)
        {

            Command currentCommand;
            if (executedCommands.Count >= number)
            {
                for (int i = 0; i < number; i++)
                {
                    currentCommand = executedCommands.Pop();
                    currentCommand.UnExecute();
                    unExecutedCommands.Push(currentCommand);
                }
            }
        }

        /// <summary>
        /// get from unexecuted add add to executed 
        /// </summary>
        /// <param name="number"></param>
        public void RedoCommands(int number)
        {
            Command currentCommand;
            if (unExecutedCommands.Count >= number)
            {

                for (int i = 0; i < number; i++)
                {
                    try
                    {
                        currentCommand = unExecutedCommands.Pop();
                        currentCommand.Execute();
                        executedCommands.Push(currentCommand);
                    }
                    catch (Exception exp)
                    {
                        throw exp;
                    }

                }

            }
        } 


    }
}
